<?php
include 'wow.html';
include 'connect_pdo.php';
include 'functions.php';
include 'api.php';

if (empty($_GET['guild'])||empty($_GET['realm']))
{
header ('Location: index.php');
}
$guild = $_GET['guild'];
$realm = $_GET['realm'];
//fix for realms with ' in name
$guild = stripslashes($guild);
$realm = stripslashes($realm);

try{
	$data = $api->getGuildApi()->getGuild($realm, $guild, true);
}
catch(exception $e)
{echo $e;}

$level = $data['level'];
$side = $data['side'];//1=horde
if ($side==1)
{$side = 'Horde';}
else
{$side = 'Alliance';}

$points = $data['achievementPoints'];
$emblem = $data['emblem']['icon'];//etc.
//get the emblem here too

$members = $data['members'];
$memberCount = count($members);

//show overall guild information
echo "<h3>$guild</h3><br/>";
echo "<h4>A level $level $side guild</h4><br/>";
echo "<p>Currently with $points points and $memberCount members</p>";

//put possible races into arrays
include 'raceClass.php';

asort($horde);
asort($alliance);
asort($class_a); 
				
				
foreach ($members as $member)//loop through all the members of the guild without using the API
{
$race = $member['character']['race'];
$class = $member['character']['class'];
$gender = $member['character']['gender'];
$level = $member['character']['level'];
$charPoints = $member['character']['achievementPoints'];
$rank = $member['rank'];
$char = $member['character']['name'];

if ($gender==0)
{$gender = 'male';}
else
{$gender = 'female';}
//set the faction and race by looking through the arrays
if (array_key_exists($race,$horde))
{$faction = 'Horde';
$race = $horde[$race];}
else
{$faction = 'Alliance';
$race = $alliance[$race];}
//set class		
$class = $class_a[$class];

//create arrays for stats
$class_ar[] = $class;
$race_ar[] = $race; 
$gender_ar[] = $gender; 
$level_ar[] = $level;
$points_ar[] = $charPoints;
$rank_ar[] = $rank;
}//end foreach members

//gender %
$charGender = array_count_values($gender_ar);
$genderPct = round((100*($charGender['male'] / count($gender_ar))),3);
echo $genderPct . "% male";

//show detailed information
echo "<form method='post'>";
echo "<input type='hidden' name='submitted' value=1/>";
echo "<input type='submit' value='Show detailed info (this can take a while!)'/>";
echo "</form>";

if ($_POST['submitted']==1)
{

echo "<br/><button id='toggle'>Show all members</button>";
echo "<br/><button id='show85'>Show only lvl 85</button>";
echo "<div class='toggle'>";

foreach ($members as $memberDetails)//loop through all members and use the API!
{
$char = $memberDetails['character']['name'];
	$race = $memberDetails['character']['race'];
	$class = $memberDetails['character']['class'];
	//set the faction and race by looking through the arrays
	if (array_key_exists($race,$horde))
	{$faction = 'Horde';
	$race = $horde[$race];}
	else
	{$faction = 'Alliance';
	$race = $alliance[$race];}
	//set class		
	$class = $class_a[$class];
	$gender = $memberDetails['character']['gender'];
	$level = $memberDetails['character']['level'];
	$charPoints = $memberDetails['character']['achievementPoints'];
	$rank = $memberDetails['rank'];
	
try {
	$character = $api->getCharacterApi()->getCharacter($realm, $char, 
		array('guild', 'stats', 'items', 'titles', 'professions', 'talents'));
		
	$itemLevel_ar[] = $character['items']['averageItemLevel'];	
	$characterTalents = $character['talents'];
	$characterProfs = $character['professions']['primary'];
	$ilevel = $character['items']['averageItemLevel'];
	
	//skill name
$prof = array ( 0 => xml_blank($characterProfs, 0, 'name'), 1 =>
		xml_blank($characterProfs, 1, 'name')
			);
//skill level
$profval = array ( 0 => xml_blank($characterProfs, 0, 'rank'), 1 =>
		xml_blank($characterProfs, 1, 'rank')
			);
	//secondary skills
	$characterSkills = $character['professions']['secondary'];

	$skill[] = xml_blank($characterSkills, 0, 'name');
	$skill[] = xml_blank($characterSkills, 1, 'name');
	$skill[] = xml_blank($characterSkills, 2, 'name');
	$skill[] = xml_blank($characterSkills, 3, 'name');
	
	$skillVal[] = xml_blank($characterSkills, 0, 'rank');
	$skillVal[] = xml_blank($characterSkills, 1, 'rank');
	$skillVal[] = xml_blank($characterSkills, 2, 'rank');
	$skillVal[] = xml_blank($characterSkills, 3, 'rank');

	$talent1 = trim(xml_blank($characterTalents, 0, 'name'));
	$talent2 = trim(xml_blank($characterTalents, 1, 'name'));
	
	if ($talent1<>'None'&&$talent1<>''&&(!empty($talent1)))
	{$talent_ar[] = $talent1;}
	
	if ($talent2<>'None'&&$talent2<>''&&(!empty($talent2)))
	{$talent_ar2[] = $talent2;}
	
	}
		
	catch(exception $e)
	{
	//echo $e; hide exceptions for inactive toons in guild
	}	
	//show detailed information - start 
	if ($level<>85)
	{echo "<div class='show85'>";}
	else
	{echo "<div>";}
	echo "Name: $char, $talent1 $race $class. Level $level<br/>";
	echo "Ach Points: ",$charPoints,"<br/>";
	echo "Item level: ",$ilevel,"<br/>";
	echo "<br/>";
	echo "</div>";
	
}//end loop through members
echo "</div>";
//skill stats
//$skill_ar = array_combine($skill, $skillVal);
//print_r($skill_ar);

//ilevel stats
$itemLevel = array_count_values($itemLevel_ar);
$ilevelSum = array_sum($itemLevel_ar);
$ilevelMax = max($itemLevel_ar);
$ilevelMin = min($itemLevel_ar);
$ilevelAvg = round(($ilevelSum / count($itemLevel_ar)),2);
echo "<h5>Item Level Data</h5>";
echo "Max ilevel: $ilevelMax<br/>";
echo "Min ilevel: $ilevelMin<br/>";
echo "Average ilevel: $ilevelAvg<br/>";

//spec stats
$specStats = array_count_values($talent_ar);
$specStats2 = array_count_values($talent_ar2);
ksort($specStats);
ksort($specStats2);

echo "<h5>Spec Data</h5>";
echo "<b>Main specs</b><br/>";
foreach ($specStats as $specStat=>$specCount)
{
echo "$specStat: $specCount<br/>";
}
echo "<br/><b>Off specs</b><br/>";
foreach ($specStats2 as $specStat2=>$specCount2)
{
echo "$specStat2: $specCount2<br/>";
}


}//end if post submitted code

//calculate level data
$charLevel = array_count_values($level_ar);
$levelMaxChars = $charLevel[90];
$levelSum = array_sum($level_ar);
$levelMax = max($level_ar);
$levelMin = min($level_ar);
$levelAvg = round(($levelSum / count($level_ar)),2);
$maxLevelPct = round((100*($levelMaxChars / count($level_ar))),3);

echo "<h5>Level Data</h5>";
echo "Max level: $levelMax<br/>";
echo "Min level: $levelMin<br/>";
echo "Average level: $levelAvg<br/>";
echo "Toons at max level: $levelMaxChars ($maxLevelPct%)<br/>";

//calculate class data
$charClass = array_count_values($class_ar);

echo "<h5>Class Data</h5>";
foreach ($class_a as $testClass)
	{
	$pct = round((100*($charClass[$testClass]/count($class_ar))),3);
	echo "$testClass: " . $charClass[$testClass] . " ($pct%)<br/>";
	}

//calculate race data
$charRace = array_count_values($race_ar);
echo "<h5>Race Data</h5>";

if ($side == 'Horde')
{
//check if any races are missing
foreach ($horde as $testRace) 
	{
	$pct = round((100*($charRace[$testRace]/count($race_ar))),3);
	echo "$testRace: " . $charRace[$testRace] . " ($pct%)<br/>";
	}
}
else 
{
//check if any races are missing
foreach ($alliance as $testRace)
	{
	echo "$testRace: " . $charRace[$testRace] . "<br/>";
	}

}

//calculate points data
$charPoints = array_count_values($points_ar);
$pointsSum = array_sum($points_ar);
$pointsMax = max($points_ar);
$pointsMin = min($points_ar);
$pointsAvg = round(($pointsSum / count($points_ar)),2);

echo "<h5>Achievement Data</h5>";
echo "Max points: $pointsMax<br/>";
echo "Min points: $pointsMin<br/>";
echo "Average points: $pointsAvg<br/>";

//calculate rank data
$charRank = array_count_values($rank_ar);
ksort($charRank);

echo "<h5>Rank Data</h5>";
foreach ($charRank as $rankOutput=>$rankCount)
	{
	echo "Rank $rankOutput: $rankCount<br/>";
	}

include 'footer.html';
?>